package com.changgou.listener;

import okhttp3.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * Created by 柱子 on 2020/10/19 19:52   RabbitMQ消息接收监听
 */


@Component
public class AdListener {

    //设置监听的队列
    @RabbitListener(queues = "ad_update_queue")
    public void receiveMessage(String message) {
        System.out.println("接收到的消息为 : " + message);


        //发起远程调用
        OkHttpClient okHttpClient = new OkHttpClient();
        //String url = "http://192.168.200.128/ad_update?position=" + message;
        String url = "http://192.168.200.128/ad_update?position="+message;
        Request request = new Request.Builder().url(url).build();

        Call call = okHttpClient.newCall(request);

        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //接收失败
                e.printStackTrace();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //接收成功
                System.out.println("接收成功" + response.message());
            }
        });
    }


}
